package com.weichen.xd;

import android.content.Intent;
import android.os.Bundle;
import android.os.HandlerThread;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.weichen.xd.data.City;
import com.weichen.xd.utils.FileUtils;

import java.io.IOException;
import java.util.List;

public class MainActivity5 extends AppCompatActivity {

    private static final String TAG = "main 4";


    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main5);
        Log.e(TAG, "onCreate：" + getClass().getSimpleName() + " TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
        Log.e(TAG, "onCreated");

        mGridView = (GridView) findViewById(R.id.grid_view);

        mGridView.setAdapter(new LAdapter());


        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });

        initCity();
    }


    private void initCity(){

        Gson gson = new Gson();
        String s = readCityList();
        if(!TextUtils.isEmpty(s)){

            JsonArray array = gson.fromJson(s, JsonObject.class).getAsJsonArray("city_info").getAsJsonArray();

            City[] arr = gson.fromJson(array, City[].class);

        }
    }

    // android  读取 txt 文件
    private String readCityList(){

        String str = null;
        try {
            str = FileUtils.readTextFile(getAssets().open("cityList.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return str;
    }

    @Override
    protected void onStart() {
        super.onStart();


        Log.e(TAG, "onStart");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Log.e(TAG, "onNewIntent");
    }
}